package kk.servlet;

import kk.domain.Book;
import kk.domain.PageBean;
import kk.service.BookService;
import kk.service.impl.BookServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/manager/bookServlet")
public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Book> books = bookService.queryBooks();
        request.setAttribute("books",books);
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String pageNoStr = request.getParameter("pageNo");
        int pageNo = Integer.parseInt(pageNoStr);
        Map<String, String[]> parameterMap = request.getParameterMap();
        Book book=new Book();
        try {
            BeanUtils.populate(book,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        bookService.addBook(book);
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&currentPage="+(pageNo+1));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String pageNoStr = request.getParameter("pageNo");
        int pageNo = Integer.parseInt(pageNoStr);
        int i = Integer.parseInt(id);
        bookService.deleteBookById(i);
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&currentPage="+pageNo);
    }

    public void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        int i = Integer.parseInt(id);
        Book book = bookService.queryBookById(i);
        request.setAttribute("book",book);
        request.getRequestDispatcher("/pages/manager/book_edit.jsp?pageNo="+pageNo).forward(request,response);
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        Book book=new Book();
        try {
            BeanUtils.populate(book,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        bookService.updateBook(book);
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&currentPage="+pageNo);
    }

    public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //默认第一页
        int currentPage=1;
        String currentPageStr = request.getParameter("currentPage");
        if (currentPageStr!=null){
            currentPage=Integer.parseInt(currentPageStr);
        }

        //默认每页显示四条数据
        int pageSize=4;
        String pageSizeStr = request.getParameter("pageSize");
        if (pageSizeStr!=null){
            pageSize=Integer.parseInt(pageSizeStr);
        }
        PageBean<Book> pageBean = bookService.page(currentPage,pageSize);
        pageBean.setUrl("/manager/bookServlet?action=page");
        request.setAttribute("pageBean",pageBean);
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }
}
